package com.etong.citymap.activity;

import java.util.ArrayList;

import com.etong.citymap.db.util.CityApplication;
import com.etong.citymap.module.SinglecallBean;
import com.etong.citymap.util.ContentProvicerUtil;
import com.etong.citymap.util.StringUtil;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 请选择要添加的号码
 */
public class AddressListActivity extends Activity {
	LinearLayout addresslistview;
	Button address_back;
	Button address_enter;
	TextView address_count;
	int count = 0;
	private CityApplication cityApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addresslist);
		cityApplication = (CityApplication)getApplication();
		addresslistview = (LinearLayout) findViewById(R.id.addresslistview);
		initdate();
		initdateview();
		address_back = (Button) findViewById(R.id.address_back);
		address_enter = (Button) findViewById(R.id.address_enter);
		address_count = (TextView) findViewById(R.id.address_count);
		address_back.setOnClickListener(onClickListener);
		address_enter.setOnClickListener(onClickListener);
	}

	void initdateview() {
		for (int i = 0; i < cityApplication.getCachecontactBeans().size(); i++) {
			SinglecallBean contactbean = cityApplication.getCachecontactBeans().get(i);
			View view = LayoutInflater.from(AddressListActivity.this).inflate(
					R.layout.listitem, null);
			ImageView imageView = (ImageView) view
					.findViewById(R.id.listitem_icon);
			imageView.setImageResource(R.drawable.ic_launcher);
			TextView name = (TextView) view.findViewById(R.id.listitem_title);
			TextView conten = (TextView) view
					.findViewById(R.id.listitem_content);
			name.setText(contactbean.getName());
			String contetnr = "手机" + " "+ contactbean.getPhone();
			conten.setText(contetnr);
			view.setTag(i + "");
			view.setOnClickListener(onClickListener);
			addresslistview.addView(view);
		}
	}

	OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getTag() != null
					&& StringUtil.IsNotNull(v.getTag().toString())) {

				SinglecallBean bean = cityApplication.getCachecontactBeans().get(Integer.valueOf(v.getTag()
						.toString()));
				if (bean.isSelect()) {
					v.setBackgroundResource(R.drawable.listback);
					bean.setSelect(false);
					if (count > 0) {
						count--;
					}
				} else {
					v.setBackgroundColor(R.drawable.listdown);
					bean.setSelect(true);
					count++;
				}
				if (count > 0) {
					address_count.setText("你选中了" + count + "个号码");
				} else {
					address_count.setText("请选择要添加的号码");
				}
			} else {
				if (address_back == v) {
					finish();
				}
				if (address_enter == v) {
					Intent data=new Intent();
                    setResult(120, data);
                    finish();
				}
			}
		}
	};

	void initdate() {
		cityApplication.getCachecontactBeans().clear();
		Cursor cursor = ContentProvicerUtil.selectData(
				AddressListActivity.this,
				ContactsContract.Contacts.CONTENT_URI, null);
		while (cursor.moveToNext()) {
			ArrayList<String> phones = new ArrayList<String>();
			long _id = cursor.getLong(cursor
					.getColumnIndex(ContactsContract.Contacts._ID));
			String name = cursor.getString(cursor
					.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
			Cursor cursor_phones = ContentProvicerUtil.selectData(AddressListActivity.this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
							+ _id);
			while (cursor_phones.moveToNext()) {
				String phone = cursor_phones.getString(cursor_phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				phones.add(phone);

			}
			cursor_phones.close();
			SinglecallBean mycontacts = new SinglecallBean((int)_id , name, phones.get(0),false);
			cityApplication.getCachecontactBeans().add(mycontacts);
		}
	}

}